package com.qbw.app.module.vehicle.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.http.okhttp.HttpTask;
import com.lib.util.StringUtil;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.VehicleActivityCoolantBinding;
import com.qbw.app.model.Coolant;
import com.qbw.app.model.Vehicle;
import com.qbw.app.module.common.activity.CommonActivity;
import com.qbw.app.module.common.util.HttpRequestUtil;
import com.qbw.app.module.common.util.UniversalToastUtil;
import com.qbw.customview.RefreshLoadMoreLayout;

import static com.qbw.app.request.impl.VehicleRequest.METHOD_COOLANT;

/**
 * 冷却液
 *
 * @author QBW
 * @date 2019/8/23
 */
public class VehicleCoolantActivity extends CommonActivity<VehicleActivityCoolantBinding> {

    private static final String KEY_COOLANT = "coolant";
    private Handler mHandler = new Handler();
    private Coolant mCoolant;

    public static void start(Context context, Coolant coolant) {
        Intent intent = new Intent(context, VehicleCoolantActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(KEY_COOLANT, coolant);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.vehicle_activity_coolant;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getIntent().getExtras() : savedInstanceState;
        mCoolant = (Coolant) args.getSerializable(KEY_COOLANT);
        Vehicle vehicle = mCoolant.getVehicle();
        if (vehicle != null) {
            mBinding.title.setSubTitle(vehicle.getVehicleNick());
            mBinding.title.setSubTitleVisible(true);
        }
        mBinding.title.setListener(this);
        mBinding.rll.init(new RefreshLoadMoreLayout.Config(this).canLoadMore(false));
        mBinding.setCoolant(mCoolant);
        mBinding.executePendingBindings();

        updateUi();
    }

    private void updateUi() {
        mBinding.tvStatus.setText(mCoolant.getStatusText());
        mBinding.ivBg.setImageResource(mCoolant.getWaveBg());
        mBinding.waveProgressbar.setBorderColor(mCoolant.getWaveFrontColor());
        mBinding.waveProgressbar.setFrontWaveColor(mCoolant.getWaveFrontColor());
        mBinding.waveProgressbar.setBehidWaveColor(mCoolant.getWaveBehindColor());
    }

    @Override
    protected void onResume() {
        super.onResume();
        int progress = (int) StringUtil.numberFloat(mCoolant.getValue());
        if (progress > 100) {
            progress = 100;
        }
        final int p = progress;
        mHandler.postDelayed(() -> mBinding.waveProgressbar.setProgress(65), 100);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mBinding.waveProgressbar.stopWaveAnimation();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable(KEY_COOLANT, mCoolant);
    }

    @Override
    public void onRefresh() {
        if(mCoolant.getVehicle() != null){
            mGeneralManager.getRequestManager()
                    .getVehicleRequest()
                    .getCoolant(mCoolant.getVehicle().getVehicleCode(), this)
                    .execute();
        }

    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        String method = httpTask.getMethod();
        if (METHOD_COOLANT.equals(method)) {
            Coolant.Response response = (Coolant.Response) entity;
            Coolant coolant = response.getData();
            if(mCoolant.getVehicleCode() != null){
                coolant.setVehicleCode(mCoolant.getVehicleCode());
                mCoolant = coolant;
            }

            mBinding.setCoolant(mCoolant);
            updateUi();
            mBinding.rll.stopRefresh();
        }
    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        super.onHttpFailed(httpTask, errorCode, message);
        message = HttpRequestUtil.getMessage(errorCode, message);
        String method = httpTask.getMethod();
        if (METHOD_COOLANT.equals(method)) {
            UniversalToastUtil.showFailedIconToast(message);
            mBinding.rll.stopRefresh();
        }

    }
}
